﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.IO;

namespace MvcSite.Models
{
    public class GestoreFoto : GestoreBase
    {
    
        /// <summary>
        /// Aggiunge al db la foto
        /// </summary>
        /// <param name="Nome"></param>
        /// <param name="Album"></param>
        public static void UploadFoto(Foto foto, HttpPostedFileBase file)
        {
            string dir = GetPathFromServer(GetPathAlbum(foto.Album.ToString()));
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            file.SaveAs(GetPathFromServer(foto.Path));
            MotoreDataBase.GestoreBase.InsertWithId<Foto>(foto);
        }

        /// <summary>
        /// Legge dal db tutto le foto appartenenti ad un album
        /// </summary>
        /// <param name="Album"></param>
        /// <returns></returns>
        public static List<Foto> LoadFoto(int album)
        {
            List<Foto> listFoto = new List<Foto>();
            listFoto = MotoreDataBase.GestoreBase.ExecuteNonQueryToList<Foto>("SelectForAlbum", new MotoreDataBase.Parameter("id", album));

            return listFoto;
        }

        /// <summary>
        /// Elimina dal db tutte le foto con id appartenente alla lista passata
        /// </summary>
        /// <param name="selected_id"></param>
        public static void DeleteFoto(string[] selected_id)
        {
            if (selected_id != null)
            {
                string list_id = String.Join(",", selected_id);
                using (SqlConnection objConn = new SqlConnection(stringConnection))
                {
                    objConn.Open();
                    string query = string.Format("DELETE FROM Foto WHERE id_img IN ({0})", list_id);
                    SqlCommand objCmd = new SqlCommand(query, objConn);

                    int n = objCmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// Elimina dal db tutte le foto appartenenti ad una determinata categoria
        /// </summary>
        /// <param name="categoria"></param>
        public static void DeleteFoto(int album)
        {
            MotoreDataBase.GestoreBase.stringConnection = stringConnection;
            int n = MotoreDataBase.GestoreBase.ExecuteNonQueryToNRow<Foto>("DeleteFromAlbum", new MotoreDataBase.Parameter("id", album));
        }

        /// <summary>
        /// Conta le foto nel db appartenenti ad una determinata categoria
        /// </summary>
        /// <param name="categoria"></param>
        /// <returns></returns>
        public static int CountFoto(int id)
        {
            int n_foto;
            using (SqlConnection objConn = new SqlConnection(stringConnection))
            {
                objConn.Open();
                string query = "SELECT FROM Foto WHERE album = @id";
                SqlCommand objCmd = new SqlCommand(query, objConn);
                objCmd.Parameters.AddWithValue("id", id);
                n_foto = objCmd.ExecuteNonQuery();
            }

            return n_foto;
        }

        public static List<Foto> GetPresentazione(int n_foto)
        {
            List<Foto> list = new List<Foto>();
            using (SqlConnection objConn = new SqlConnection(stringConnection))
            {
                objConn.Open();
                string query = "SELECT TOP (@n_foto) * FROM Foto ORDER BY data_upload DESC";
                SqlCommand objCmd = new SqlCommand(query, objConn);
                objCmd.Parameters.AddWithValue("n_foto", n_foto);
                using (SqlDataReader reader = objCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        list.Add(new Foto(reader));
                    }
                }
            }
            return list;
        }


    }
}